package com.nail.api.customMake.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.nail.api.customMake.bean.Address;
import com.nail.api.customMake.bean.Country;
import com.nail.api.customMake.service.IAddressService;
import com.nail.api.user.bean.User;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.controller.BaseController;
import com.nail.utils.IPUtils;
import com.nail.utils.PageResult;
import com.nail.utils.Result4API;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/address")
public class AddressController extends BaseController{
    Logger logger = Logger.getLogger(this.getClass());

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private IAddressService addressService;

    @PostMapping(value="/addAddress")
    @ResponseBody
    public Result4API addAddress(HttpServletRequest request, Address address,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：addAddress" );

        if(address == null || address.getUserId() == null || StringUtils.isEmpty(address.getAddress()) || StringUtils.isEmpty(address.getEmail())
                || StringUtils.isEmpty(address.getMobile())|| StringUtils.isEmpty(address.getName()) || StringUtils.isEmpty(address.getPostalCode()) ){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
        address.setUserId(user.getId());
        address.setUserName(user.getUserName());
        address.setCreateTime(new Date());

        try {
            addressService.addAddress(address);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }

    @PostMapping(value="/deleteAddress")
    @ResponseBody
    public Result4API deleteAddress(HttpServletRequest request, Long addressId, String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：deleteAddress" );

        if(addressId == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        try {
            User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
            addressService.deleteAddress(user.getRole().getRoleType() == 0 ? user.getId():null,addressId);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }

    @PostMapping(value="/updateAddress")
    @ResponseBody
    public Result4API updateAddress(HttpServletRequest request,Address address,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：updateAddress" );

        if(address ==null || address.getId()==null ){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        try {
            addressService.updateAddress(address);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }

    @PostMapping(value="/getAddress")
    @ResponseBody
    public Result4API getAddress(HttpServletRequest request,String token,Long userId,Long addressId){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getAddress" );

        if(StringUtils.isEmpty(token) || userId == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
        try {
            //管理员不过滤
            List<Address> list = addressService.getAddress(userId,user.getRole().getRoleType() == 0 ? user.getId():null,addressId);
            result.setData(list);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }
    
    @PostMapping(value="/getCountry")
    @ResponseBody
    public Result4API getCountry(HttpServletRequest request,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getCountry" );
        try {
           
        	List<Country> list = addressService.getCountry();
            result.setData(list);
            result.setCode(Constant.API_CODE_SUCCESS);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }
    
}
